package com.wh.concurrent.threadlocal;

public class TestExpand {

	private ThreadLocal<Student> student = new ThreadLocal<Student>();
	
	public Student getStudent() {
		return this.student.get();
	}

	public void setStudent(Student student) {
		this.student.set(student);
	}

	
	public static void main(String[] args) {
		final TestExpand testExpand = new TestExpand();
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				Student student = new Student();
				student.setId("1");
				student.setName("wanghao");
				testExpand.setStudent(student);
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + " " + testExpand.getStudent());
			}
		};
		
		Runnable runnable1 = new Runnable() {
			
			@Override
			public void run() {
				Student student = new Student();
				student.setId("2");
				student.setName("zdw");
				testExpand.setStudent(student);
				System.out.println(Thread.currentThread().getName() + " " + testExpand.getStudent());
			}
		};
		new Thread(runnable).start();
		new Thread(runnable1).start();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + " " + testExpand.getStudent());
	}
}

class Student {

	private String id;
	private String name;
	
	public Student() {
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	
}
